package com.esint;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * @description: redis 事务测试类
 * @author: 谷明
 * @createDate: 2021/8/12 2:43
 */
public class TestTx {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.43.166", 6379);
        jedis.flushDB();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello", "world");
        jsonObject.put("name", "esint");
        //开启事务
        Transaction multi = jedis.multi();
        String result = jsonObject.toJSONString();
        try{
            multi.set("user1", result);
            multi.set("user2", result);
            int i = 1 / 0;
            multi.exec();
        } catch (Exception e){
            multi.discard();
        } finally {
            jedis.close();
        }
    }
}
